﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntitiesLayer;
using System.Collections;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;

namespace DAL.Repositories
{
    public class HorarioRepository : IRepository<Horario>
    {

        private List<Horario> insertItems;
        private List<Horario> deleteItems;
        private List<Horario> updateItems;

        public HorarioRepository()
        {
            insertItems = new List<Horario>();
            updateItems = new List<Horario>();
            deleteItems = new List<Horario>();
        }

        public void InsertHorario(Horario horario)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.Parameters.Add(new SqlParameter("@dia", horario.Dia));
                cmd.Parameters.Add(new SqlParameter("@horaEntrada", Convert.ToDateTime(horario.HoraEntrada)));
                cmd.Parameters.Add(new SqlParameter("@horaSalida", Convert.ToDateTime(horario.HoraSalida)));

                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_agregar_horario");
            }
            catch (Exception ex)
            {

            }

        }

        public void Save()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    if (insertItems.Count > 0)
                    {
                        foreach (Horario horario in insertItems)
                        {
                            InsertHorario(horario);
                        }
                    }

                    if (updateItems.Count > 0)
                    {
                        foreach (Horario horario in updateItems)
                        {
                            //UpdateUsuario(usuario);
                        }
                    }

                    if (deleteItems.Count > 0)
                    {
                        foreach (Horario horario in deleteItems)
                        {
                            //DeleteUsuario(usuario);
                        }
                    }

                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {

                }
                catch (ApplicationException ex)
                {

                }
                finally
                {
                    Clear();
                }
            }
        }

        public void Clear()
        {
            insertItems.Clear();
            deleteItems.Clear();
            updateItems.Clear();
        }

        public void Insert(Horario entity)
        {
            insertItems.Add(entity);
        }

        public void Delete(Horario entity) { }
        public void Update(Horario entity) { }

        public IEnumerable<Horario> GetAll() { return null; }
        public Horario GetById(int id) { return null; }
    }
}